package com.yjhb.android.sbz.activity;

import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.activity.rule.RuleReputationActivity;
import com.yjhb.android.sbz.adapter.ReputationAdapter;
import com.yjhb.android.sbz.tool.EventMsg;
import com.yjhb.android.sbz.manager.ResManager;
import com.jojo.android.zxlib.base.BaseRecyclerAdapter;
import com.jojo.android.zxlib.base.mvp.BasePresenter;
import com.jojo.android.zxlib.tool.ZxActivityTool;
import com.qmuiteam.qmui.widget.popup.QMUIListPopup;
import com.qmuiteam.qmui.widget.popup.QMUIPopup;
import com.tencent.qcloud.tim.uikit.component.action.PopMenuAction;
import com.tencent.qcloud.tim.uikit.component.action.PopMenuAdapter;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者 zx
 * 创建时间 2021/1/5 10:46
 */
public class ReputationActivity extends BaseActivity implements BaseRecyclerAdapter.ItemClickListener<ReputationAdapter.Reputation> {
    private final String TAG = "ReputationActivity";
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.title_right_tv)
    TextView title_right_tv;

    ReputationAdapter adapter;
    List<ReputationAdapter.Reputation> reputations = new ArrayList<>();

    @Override
    protected BasePresenter initPresenter() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_reputation;
    }

    @Override
    protected void initUI() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        recyclerView.setAdapter(adapter = new ReputationAdapter(this));
    }

    @Override
    protected void initData() {
        reputations.clear();
        ReputationAdapter.Reputation reputation = new ReputationAdapter.Reputation();
        reputation.setIcon(R.mipmap.level_0);
        reputation.setName("小白用户");
        reputation.setScore("信誉积分<1");
        reputations.add(reputation);

        reputation = new ReputationAdapter.Reputation();
        reputation.setIcon(R.mipmap.level_1);
        reputation.setName("书童用户");
        reputation.setScore("信誉积分1——100");
        reputations.add(reputation);

        reputation = new ReputationAdapter.Reputation();
        reputation.setIcon(R.mipmap.level_2);
        reputation.setName("秀才用户");
        reputation.setScore("信誉积分100——1000");
        reputations.add(reputation);

        reputation = new ReputationAdapter.Reputation();
        reputation.setIcon(R.mipmap.level_3);
        reputation.setName("举人用户");
        reputation.setScore("信誉积分1000——2000");
        reputations.add(reputation);

        reputation = new ReputationAdapter.Reputation();
        reputation.setIcon(R.mipmap.level_4);
        reputation.setName("进士用户");
        reputation.setScore("信誉积分2000——10000");
        reputations.add(reputation);


        reputation = new ReputationAdapter.Reputation();
        reputation.setIcon(R.mipmap.level_5);
        reputation.setName("探花用户");
        reputation.setScore("信誉积分10000——50000");
        reputations.add(reputation);


        reputation = new ReputationAdapter.Reputation();
        reputation.setIcon(R.mipmap.level_6);
        reputation.setName("榜眼用户");
        reputation.setScore("信誉积分50000——100000");
        reputations.add(reputation);


        reputation = new ReputationAdapter.Reputation();
        reputation.setIcon(R.mipmap.level_7);
        reputation.setName("状元用户");
        reputation.setScore("信誉积分>100000");
        reputations.add(reputation);

        adapter.setData(reputations);
    }


    /**
     * 规则弹窗
     */
    private void showPopupRules() {
        PopMenuAdapter popMenuAdapter = new PopMenuAdapter() {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view1 = super.getView(position, convertView, parent);
                LinearLayout v = (LinearLayout) ((RelativeLayout) view1).getChildAt(0);
                v.getLayoutParams().height = 30 * ResManager.dp();
                TextView textView = view1.findViewById(com.tencent.qcloud.tim.uikit.R.id.pop_menu_label);
                textView.setGravity(Gravity.CENTER);
                textView.setTextSize(12);
                return view1;
            }
        };
        QMUIListPopup popup = new QMUIListPopup(this, QMUIPopup.DIRECTION_NONE, popMenuAdapter);
        popup.create(ResManager.dp() * 80, ResManager.dp() * 120, new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        ZxActivityTool.skipActivity(getActivity(), RuleReputationActivity.class);
                        break;
                    case 1:
                        ZxActivityTool.skipActivity(getActivity(), ReputationDetailedActivity.class);
                        break;
                }
                popup.dismiss();
            }
        });
        List<PopMenuAction> actions = new ArrayList<>();
        PopMenuAction action = new PopMenuAction();
        action.setActionName("信誉规则");
        actions.add(action);
        action = new PopMenuAction();
        action.setActionName("信誉明细");
        actions.add(action);
        popMenuAdapter.setDataSource(actions);
        popup.show(title_right_tv);
    }

    @Override
    protected void regEvent() {
        adapter.setOnItemClick(this);
    }

    @OnClick(R.id.back)
    void back() {
        finish();
    }

    @OnClick(R.id.title_right_tv)
    void title_right_tv() {
        showPopupRules();
    }


    @Override
    public void onItemClick(int index, ReputationAdapter.Reputation reputation) {

    }

    @Override
    public void onViewClick(View view, String msg, int index, ReputationAdapter.Reputation reputation) {
        if (msg.equals("button")) {
            ZxActivityTool.skipActivity(this, MainActivity.class);
            EventBus.getDefault().post(new EventMsg(EventMsg.REQUEST_MAIN_SWITCH_FRAGMENT, 1));
        }
    }
}
